<template>
  <div class="add-role">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ titleName }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="step-container">
        <!--
             direction="vertical" :标识为竖型进度条
        -->
        <el-steps direction="vertical" :active="stepActive">
          <el-step title="角色信息" />
          <el-step title="权限信息" />
          <el-step title="检查并完成" />
        </el-steps>
      </div>

      <div v-show="stepActive === 1" class="form-container">
        <div class="title">角色信息</div>
        <div class="form">
          <el-form ref="form" :model="roleNameInfo" class="myRoleForm">
            <el-form-item label="角色名称">
              <el-input v-model="roleNameInfo.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleNameInfo.remark" />
            </el-form-item>
          </el-form>
        </div>
      </div>

      <div v-show="stepActive === 2" class="form-container">
        <div class="title">权限配置</div>
        <div class="form">
          <div class="mybox">
            <div v-for="item in roleAllinfo" :key="item.id" class="roleTitle">
              <h3>{{ item.title }}</h3>
              <!--
               :props="props" 表示数据中的显示与获取的接口数据关联
                :data="item.children" 显示的数据
                show-checkbox 是否显示复选框
                node-key="id" 表示树节点的唯一标识
                :default-expand-all="true"  是否展开所有节点
                :check-strictly="true" 父子节点是否关联
              -->
              <el-tree
                ref="roletreeForm"
                :props="props"
                :data="item.children"
                show-checkbox
                node-key="id"
                :default-expand-all="true"
                :check-strictly="true"
              />
            </div>
          </div>

        </div>
      </div>

      <div v-show="stepActive === 3" class="form-container">
        <div class="title">检查并完成</div>
        <div class="form">
          <div class="title">
            <h3> <span>角色名称:</span> <span>{{ roleNameInfo.roleName }}</span> </h3>
            <h3> <span>角色描述:</span> <span>{{ roleNameInfo.remark }}</span> </h3>
          </div>
          <div class="mybox">
            <div v-for="item in roleAllinfo" :key="item.id" class="roleTitle">
              <h3>{{ item.title }}</h3>
              <!--
               :props="props" 表示数据中的显示与获取的接口数据关联
                :data="item.children" 显示的数据
                show-checkbox 是否显示复选框
                node-key="id" 表示树节点的唯一标识
                :default-expand-all="true"  是否展开所有节点
                :check-strictly="true" 父子节点是否关联
              -->
              <el-tree
                ref="roletreeFormTree2"
                :props="echoDisplayProps"
                :data="item.children"
                show-checkbox
                node-key="id"
                :default-expand-all="true"
                :check-strictly="true"
              />
            </div>
          </div>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button :disabled="stepActive === 1" @click="last">上一步</el-button>
        <el-button type="primary" @click="next">{{ stepStr }}</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addRoleApi, roleEditInfoApi, roleShowInfoApi, roleTreeperisionApi } from '@/apis/system'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      // 进度条的位置
      stepActive: 1,
      //   添加角色描述信息
      roleNameInfo: {
        // 角色名称
        roleName: '',
        // 角色描述
        remark: ''
      },
      //   所有管理信息
      roleAllinfo: [],
      //   匹配树节点与自己数据的映射
      props: {
        children: 'children',
        label: 'title'
      },
      //   获取选中的树形节点数据
      getRoleTreeList: [],
      //   确认信息中的所有数据树节点与自己数据的映射
      echoDisplayProps: {
        children: 'children',
        label: 'title',
        disabled: () => true
      }
    }
  },
  computed: {
    stepStr: {
      get() {
        return this.stepActive === 3 ? '已完成' : '下一步'
      }
    },
    titleName: {
      get() {
        return this.$route.query.roleId ? '编辑角色' : '添加角色'
      }
    }
  },
  async created() {
    // 获取项目配置信息列表
    await this.getRoleinfoFn()

    if (this.$route.query.roleId) {
      // 如果是编辑页面则回显信息
      await this.getRoleShowInfo()
    }
  },
  methods: {
    //   进度条显示上一步
    last() {
      this.stepActive > 1 && this.stepActive--
    },
    //   进度条显示下一步
    async next() {
      if (this.stepActive === 2) {
        this.getRoleTreeList = []
        this.$refs.roletreeForm.forEach(item => {
          this.getRoleTreeList.push(item.getCheckedKeys())
        })

        this.$refs.roletreeFormTree2.forEach((item, index) => {
          item.setCheckedKeys(this.getRoleTreeList[index])
        })

        const isNull = this.getRoleTreeList.every(item => item.length === 0)
        if (isNull) {
          Message.error('请至少选择一个权限！')
          return
        }
      }

      if (this.stepActive === 3) {
        // 没有roleId为添加
        if (!this.$route.query.roleId) {
          const res = await addRoleApi({
            ...this.roleNameInfo,
            perms: this.getRoleTreeList
          })
          Message.success(res.msg)
        } else {
          const res = await roleEditInfoApi({
            ...this.roleNameInfo,
            roleId: this.$route.query.roleId,
            perms: this.getRoleTreeList
          })
          Message.success(res.msg)
        }
        this.$router.back()
      }

      this.stepActive !== 3 && this.stepActive++
    },
    // 获取项目的权限信息，赋予树形控件
    async getRoleinfoFn() {
      const res = await roleTreeperisionApi()
      this.roleAllinfo = res.data
    },
    // 编辑页面回显信息
    async getRoleShowInfo() {
      const res = await roleShowInfoApi(this.$route.query.roleId)
      this.roleNameInfo.roleName = res.data.roleName
      this.roleNameInfo.remark = res.data.remark

      this.$refs.roletreeForm.forEach((item, index) => {
        item.setCheckedKeys(res.data.perms[index])
      })
    }
  }

}
</script>
  <style scoped lang="scss">
  .title {
    h3{
        font-size: 12px;
        font-weight: normal;
        margin: 0;
        padding: 0;
        line-height: 1.4;
    }
  }
  .mybox {
    display: flex;
    justify-content: space-evenly;
    height: 100%;
    .roleTitle{
        flex: 1;
        text-align: center;
        color:#666;
        h3 {
            padding: 5px 20px;
            border:  1px solid #ccc;
        }
    }
  }
  .add-role {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      justify-content: space-between;
      padding: 0 20px;
      height: 64px;
      line-height: 64px;
      font-size: 16px;
      font-weight: bold;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }

        .arrow {
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      position: fixed;
      top: 64px;
      bottom: 88px;
      width: 100%;
      overflow-y: auto;
      background: #f4f6f8;
      padding: 20px 230px;
      display: flex;
      .step-container {
        height: 400px;
        width: 200px;
      }
      .form-container {
        flex:1;
        background-color: #fff;
        overflow-y: auto;
        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }
        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;
          .el-form {
            display: flex;
            flex-wrap: wrap;
          }
          .info{
            font-size:14px;
            color:#666;
            .form-item{
              margin-bottom:20px;
            }
          }
        }
        .form-box{
          width: 600px;
          display: flex;
          flex-direction: column;
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
      z-index: 10001;
    }
    .tree-wrapper {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        .tree-item {
          flex: 1;
          padding: 0px 4px;
          text-align: center;
          .tree-title {
            background: #f5f7fa;
            text-align: center;
            padding: 10px 0;
            margin-bottom: 12px;
          }
        }
      }
  }
  .myRoleForm {
    display: block !important;
  }
  </style>
